/*
 * Project Name : NOBUNAGA
 * Created on 2007/03/17
 */
package jp.sengoku.oda {
	import mx.controls.DateChooser;
	import mx.rpc.http.HTTPService;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.events.FaultEvent;
	import flash.net.URLRequest;
	import com.adobe.xml.syndication.atom.Atom10;
	import flash.utils.Dictionary;
	import com.adobe.xml.syndication.generic.Atom10Item;
	import com.adobe.xml.syndication.atom.Entry;
	import mx.formatters.DateFormatter;
	import mx.core.mx_internal;
	import mx.events.CalendarLayoutChangeEvent;
	import mx.core.UITextField;
	use namespace mx_internal;

	/**
	 * 日本の祝祭日を色を変えて表示するDateChooser
	 * 
	 * @author	morimotohiroki@gmail.com
	 */
	public class OichiCalender extends DateChooser {

		// HTTPService
		private var rest:HTTPService;

		// Googleカレンダー(日本の休日XMLのURL)
		private const GOOGLE_URL:String = "http://www.google.com/calendar/feeds/japanese__ja%40holiday.calendar.google.com/public/basic?max-results=50";//"http://www.google.com/calendar/feeds/japanese__ja%40holiday.calendar.google.com/public/basic";

		// 祝日の一覧
		private var holidayDictionary:Dictionary;
		
		/**
		 * コンストラクタでござる
		 * 
		 */
		public function OichiCalender() {
			//TODO: implement function
			super();
			
			rest = new HTTPService();
			
			rest.url = GOOGLE_URL;
			rest.resultFormat = "text";
			rest.addEventListener(ResultEvent.RESULT, resultHandler);
			rest.addEventListener(FaultEvent.FAULT, faultHandler);

			rest.send();
			
		}

		/**
		 * Googleからのレスポンスを処理するハンドラ
		 * 
		 * @param	event	ResultEvent
		 * @return	void
		 */		
		private function resultHandler(event:ResultEvent):void {
			var holidayXML:Atom10 = new Atom10()
			holidayXML.parse(event.result as String);
			
			holidayDictionary = new Dictionary();
			var pattern:RegExp = new RegExp("\\d{4}-\\d{2}-\\d{2}", "i");
			var pattern2:RegExp = new RegExp("\\d{4}\\/\\d{2}\\/\\d{2}", "i");
			
			var df:DateFormatter = new DateFormatter();
			df.formatString = "YYYY/MM/DD";
			
			for each (var e:Entry in holidayXML.entries) {

				var c:String = e.content.value;

				var re:Array = pattern.exec(c);
				var re2:Array = pattern2.exec(c);
				
				// 繰り返し
				if (re != null) {
					var d:Date = new Date(re[0].toString().split("-").join("/"));
					
					for (var i:Number = 2000; i < 2010; i ++) {
						d.setFullYear(i);
						holidayDictionary[df.format(d)] = e.title;
					}
					
				}
				// 単独
				if (re2 != null) {
					var d2:Date = new Date(re2[0].toString());
					holidayDictionary[df.format(d2)] = e.title;
				}
				
			}
			
			invalidateDisplayList();
			
		}

		/**
		 * Googleからのレスポンスを処理するハンドラ
		 * 
		 * @param	event	FaultEvent
		 * @return	void
		 */		
		private function faultHandler(event:FaultEvent):void {
			
		}
		
	
		/**
		 * override updateDisplayList
		 * 
		 * @param	unscaledWidth	幅
		 * @param	unscaledHeight	高さ
		 * @return	void
		 */		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
			super.updateDisplayList(unscaledWidth, unscaledHeight);

			if (holidayDictionary != null) {	
				var df:DateFormatter = new DateFormatter();
				df.formatString = "YYYY/MM/DD";
				
				for (var i:Number = 0; i < dateGrid.dayBlocksArray.length; i++) {
					for each (var o:Object in dateGrid.dayBlocksArray[i]) {
						var t:UITextField = UITextField(o);
						if (t != null) {						
							if (parseInt(t.text) > 0) {
								var d:Date = new Date(dateGrid.displayedYear, dateGrid.displayedMonth, parseInt(t.text));
	
								if (holidayDictionary[df.format(d)] != null) {
									trace(dateGrid.displayedYear, dateGrid.displayedMonth, t.text, holidayDictionary[df.format(d)]);
									t.background = true;
									t.backgroundColor = 0xFFCCCC;
								} else {
									t.background = false;
								}
							} else {
								t.background = false;
							}					
						}
					}			
				}			
			}
		}
		
	}
}